package org.uniport.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.uniport.dao.IPhotoDAO;
import org.uniport.vo.Photo;

public class PhotoDAOImpl implements IPhotoDAO{
	private Connection conn = null;
    private PreparedStatement pstmt = null;
    ResultSet rs = null;
    public PhotoDAOImpl(Connection conn){
            this.conn = conn;
    }

	public boolean addPhoto(Photo photo) throws Exception {
		boolean flag = false;
		String sql = "INSERT INTO photo(pho_addr) VALUES(?)";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setString(1, photo.getPho_addr());
		if(pstmt.executeUpdate()!=0){
			flag = true;
		}
		return flag;
	}


	public int findPhotoID(String addr) throws Exception {
		int photoID = -1;
		String sql = "SELECT pho_id FROM photo WHERE pho_addr=?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setString(1, addr);
		rs = this.pstmt.executeQuery();
		if(rs.next()){
			photoID = getPhotoIDFromRs(rs);
		}
		this.pstmt.close();
		return photoID;
	}
	
	private int getPhotoIDFromRs(ResultSet rs){
		int photoID = -1;
		try {
			photoID = rs.getInt("pho_id");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return photoID;
	}
}
